﻿namespace PageAdmin.Utils
{
    /// <summary>
    /// 用户Api相关的帮助类
    /// </summary>
    public class UserApiHelper
    {

        /// <summary>
        ///  UserToken的Api地址,用于前端通过jsonp请求获取UserToken
        /// </summary>
        /// <param name="isAdmin">是否是管理员</param>
        /// <param name="domain"></param>
        /// <returns></returns>
        public static string UserTokenApi(bool isAdmin,string domain = null)
        {
            SystemConfig systemConfig = SystemConfig.Get();
            if (domain==null)
            {
                domain = systemConfig.Domain;
                string adminDomain = systemConfig.AdminDomain;
                string memberDomain = systemConfig.MemberDomain;
                if (isAdmin)
                {
                    if (!string.IsNullOrEmpty(adminDomain))
                    {
                        domain = adminDomain;
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(memberDomain))
                    {
                        domain = memberDomain;
                    }
                }
            }
            if (string.IsNullOrEmpty(domain))
            {
                domain = RequestUrlHelper.GetDomain(false);
            }
            if(!domain.EndsWith("/"))
            {
                domain = domain + "/";
            }
            string api = domain + "e/GetUserToken/";
            return api;
        }


        /// <summary>
        /// 获取登录页面地址
        /// </summary>
        /// <param name="isAdmin"></param>
        /// <returns></returns>
        public static string LoginUrl(bool isAdmin = false, SystemConfig systemConfig=null)
        {
            if(systemConfig==null)
            {
                systemConfig = SystemConfig.Get();
            }
            string domain = systemConfig.Domain;
            string adminDomain = systemConfig.AdminDomain;
            string memberDomain = systemConfig.MemberDomain;
            string url;
            if (isAdmin)
            {
                url = domain + "/" + systemConfig.AdminPath;
                if (!string.IsNullOrEmpty(adminDomain))
                {
                    url = adminDomain;
                }
            }
            else
            {
                url = domain + "/Member";
                if (!string.IsNullOrEmpty(memberDomain))
                {
                    url = memberDomain;
                }
            }
            return url;
        }

    }
}
